<?php
class CommonAction extends Action
{
	protected function _initialize()
	{
		if (C("siteClose") == 1) {
			echo "<meta charset=\"utf-8\" />";
			exit("网站维护中");
		}
		C("URL_MODEL", 3);
		$userAgent=$_SERVER['HTTP_USER_AGENT'];
		$isApp=false;
		if(stristr($userAgent, 'Html5Plus'))$isApp=true;//Android
		//ios
		if(stristr($userAgent, 'iPhone'))
		{
			if(stristr($userAgent, 'Mobile'))
			{
				if(stristr($userAgent, 'Safari'))
				{
					if(stristr($userAgent, 'QQ') || stristr($userAgent, 'MicroMessenger') || stristr($userAgent, '360'))
					{
						
					}else{
						$isApp=true;//iso
					}
					
				}
				
			}
			
		}
			
		
		if(!$isApp){
			
			require "index.html";
			exit();
		}
		/*
		import('ORG.Net.IpLocation');
        $Ip = new IpLocation(); // 实例化类
        $area = $Ip->getlocation(); // 获取域名服务器所在的位置
        $country=iconv("GB2312","UTF-8",$area['country']);
		$sql = "insert ignore into __PREFIX__agent(ip,ipname,agent,referer,last_time)value('".get_client_ip()."','".$country."','".$_SERVER['HTTP_USER_AGENT']."','".$_SERVER['HTTP_REFERER']."','".time()."')";
		$res = M()->execute($sql);
		*/
		$action = strtoupper(MODULE_NAME . "@" . ACTION_NAME);
		$oaction = strtoupper(MODULE_NAME . "@*");
		$allowlist = array("INDEX@*", "SMS@*", "PAGE@*", "CALLBACK@*", "PAY@*");
		if (!in_array($action, $allowlist) && !in_array($oaction, $allowlist) && !$this->isLogin()) {
			$this->redirect("Index/login");
			exit(0);
		}		
		$loanbillModel = D("Loanbill");
		$loanorderModel = D("Loanorder");
		$w = array("repayment_time" => array("LT", time()), "status" => array("IN", "0,1"), "overdue_settime" => array("LT", time() - 24 * 60 * 60));
		//$overdueList = $loanbillModel->where($w)->select();
		//var_dump($loanbillModel->getLastSql());die;
		$i = 0;
		while ($i < count($overdueList)) {
			$bill = $overdueList[$i];
			$order = $loanorderModel->where(array("id" => $bill["toid"]))->find();
			if ($order) {
				$overdue_interest = $order["overdue"];
				$overdue_days = abs(intval(diffBetweenTwoDays(time(), $bill["repayment_time"])));
				$overdue_money = toMoney($bill["money"] * $overdue_interest * $overdue_days);
				//var_dump(diffBetweenTwoDays($bill["repayment_time"],time()));die;
				$loanbillModel->where(array("id" => $bill["id"]))->save(array("status" => 1, "overdue" => $overdue_money, "overdue_settime" => time()));
				$userModel = D("User");
				$number = $userModel->getInfo("id", $bill["uid"], "telnum");
				$smsModel = D("Sms");
				$content = htmlspecialchars_decode(htmlspecialchars_decode(C("bill_overdue")));
				$content = str_replace("<@>", $bill["oid"], $content);
				$content = str_replace("《@》", $bill["oid"], $content);
				$content = str_replace("<@num@>", $bill["billnum"], $content);
				$content = str_replace("《@num@》", $bill["billnum"], $content);
				//$smsModel->sendSms($number, $content);
			}
			$i = $i + 1;
		}
		$hasNearTime = strtotime("+3 Day", time());
		$w = array("repayment_time" => array("LT", $hasNearTime), "status" => 0, "overdue_smsstatus" => 0);
		//$overdueList = $loanbillModel->where($w)->select();
		$i = 0;
		while ($i < count($overdueList)) {
			$bill = $overdueList[$i];
			$userModel = D("User");
			$number = $userModel->getInfo("id", $bill["uid"], "telnum");
			$smsModel = D("Sms");
			$content = htmlspecialchars_decode(htmlspecialchars_decode(C("bill_remind")));
			$content = str_replace("<@>", $bill["oid"], $content);
			$content = str_replace("《@》", $bill["oid"], $content);
			$content = str_replace("<@num@>", $bill["billnum"], $content);
			$content = str_replace("《@num@》", $bill["billnum"], $content);
			//$smsModel->sendSms($number, $content);
			$i = $i + 1;
		}				
		return NULL;
	}
	protected function isLogin()
	{
		@($userinfo = cookie("user"));
		if($userinfo){
			$user = M('User')->where(array('id'=>$userinfo['id']))->find();
			cookie("user", $user,24*3600*30);
			$userinfo = cookie("user");
		}
		return empty($userinfo) ? false : $userinfo;
	}
	protected function setLogin($userinfo)
	{
		if (empty($userinfo)) {
			cookie("user", NULL);
			return true;
		}
		if (!empty($userinfo['password'])) {
			unset($userinfo['password']);
		}
		cookie("user", $userinfo,24*3600*30);
		return true;
	}
}